****************************************************************************************
*Table A14: Robots and college enrollment: Exclusion of CZs with highest robot exposure*
****************************************************************************************

*Education
use "$final_data_outcomes/czone_Students.dta", clear
merge 1:1 czone year using "$final_data_outcomes/IPEDS_EnrollmentCZ.dta", assert(1 3) nogenerate

*Automation
merge 1:1 czone year using "$final_data_automation/czones_ExposureAutomation.dta", assert(1 3) nogenerate

*Imports merge
merge 1:1 czone year using "$final_data_imports/czone_ExposureChina", assert(1 3) nogenerate

*Covariates
merge m:1 czone using "$final_data_covariates/czone1990_Covariates.dta", assert(3) nogenerate //beginning of period
merge m:1 czone using "$final_data_covariates/IPEDS_institutionControlsCZ.dta", assert(1 3) nogenerate

*Keep only periods of interest
keep if year<=2000

xtset czone year

*Exclude Detroit
gen ind_expof_us_adj_det=(czone==11600)

*Exclude top 1% in each period
gen ind_expof_us_adj_top1 = 0

foreach year in 1990 2000 {
qui sum expof_us_adj if year==`year', d
replace ind_expof_us_adj_top1 = 1 if expof_us_adj>=`r(p99)' & year==`year'
}

*Exclude CZs around Great Lakes with largest exposure
gen ind_expof_us_adj_great_lakes = (statefip==18|statefip==26|statefip==39)

*Build exposure measures for areas
foreach top in det top1 great_lakes {
gen expof_us_adj_`top' = expof_us_adj if ind_expof_us_adj_`top'==0
gen d_exp_us_stacked_`top' = d_exp_us_stacked if ind_expof_us_adj_`top'==0
gen expof_euro7_qo_`top' = expof_euro7_qo if ind_expof_us_adj_`top'==0
gen d_exp_iv_stacked_`top' = d_exp_iv_stacked if ind_expof_us_adj_`top'==0
}

foreach var in public private uniTop30Dummy largest150 anyAid {
replace `var' = 0 if mi(`var')
}

egen medianAnyAid = median(anyAid), by(statefip)
	replace anyAid = medianAnyAid if mi(anyAid)

eststo clear

*Exclude Detroit
eststo: ivreghdfe d_sc (expof_us_adj_det d_exp_us_stacked_det = expof_euro7_qo_det d_exp_iv_stacked_det) d_sc_70_90 $occupations_1990 $demographics_1990 $industry_shares_1990 $institutions_1990 if ind_expof_us_adj_det==0 [w=ipums_pop_1990], absorb(division##year statefip) cluster(statefip)

*Exclude top 1 percent
eststo: ivreghdfe d_sc (expof_us_adj_top1 d_exp_us_stacked_top1 = expof_euro7_qo_top1 d_exp_iv_stacked_top1) d_sc_70_90 $occupations_1990 $demographics_1990 $industry_shares_1990 $institutions_1990 if ind_expof_us_adj_top1==0 [w=ipums_pop_1990], absorb(division##year statefip) cluster(statefip)

*Exclude Great Lakes
eststo: ivreghdfe d_sc (expof_us_adj_great_lakes d_exp_us_stacked_great_lakes = expof_euro7_qo_great_lakes d_exp_iv_stacked_great_lakes) d_sc_70_90 $occupations_1990 $demographics_1990 $industry_shares_1990 $institutions_1990 if ind_expof_us_adj_great_lakes==0 [w=ipums_pop_1990], absorb(division##year statefip) cluster(statefip)

estout using "$tables_appendix/Table A13.tex", style(tex) rename(expof_us_adj_det expof_us_adj expof_us_adj_top1 expof_us_adj expof_us_adj_great_lakes expof_us_adj) ///
varlabels(expof_us_adj "US robot exposure") ///
cells(b(fmt(%9.3f) star) se(par)) nolabel replace mlabels(none) collabels(none) starlevels(\$^{*}\$ .1 \$^{**}\$ .05 \$^{***}\$ .01) /// 
keep(expof_us_adj) ///
order(expof_us_adj)
